package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.User;
import com.atguigu.springcloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping("/select")
    public User select(Long id){
        User user = userService.select(id);
        String name = user.getName();
        user.setName(name+serverPort);
        return user;
    }
    @RequestMapping("/insert")
    public int insert(@RequestBody User user){
        return userService.insert(user);
    }
    @RequestMapping("/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for(String element : services){
            log.info("------: " + element);
        }
        List<ServiceInstance> consumeservice = discoveryClient.getInstances("CONSUMESERVICE");
        for(ServiceInstance instance : consumeservice){
            log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
}
